﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
//TODO:  Use two media elements in ping-pong mode. Then one ends the second should be primed (pre-buffered) and ready to play.

/*
 *   Usage:
 *   private Sound sound;
 *   sound = new Sound("Ignition_rev.mp3", 400, 300, 6, false);
 *   sound.Loop(true);
 *   LayoutRoot.Children.Add(sound.getMediaElement());
 *   sound.PlaySound();
 */
namespace ManPowerEngine
{
    public class Sound : ComponentBase
    {
        private MediaElement mediaElement;
        Duration len;
        DispatcherTimer dp = new DispatcherTimer();
        TimeSpan offset = TimeSpan.FromMilliseconds(500);
        /// <summary>
        /// Function to access media element
        /// </summary>
        /// <returns>Media Element</returns>
        public MediaElement getMediaElement()
        {
            return mediaElement;
        }

        /// <summary>
        /// initialize a Soudn Engine
        /// </summary>
        /// <param name="uri">URI of this Sound</param>
        /// <param name="width">Width in pixel</param>
        /// <param name="height">Height in pixel</param>
        /// <param name="zindex">Z-Index. The bigger the higher.</param>


        public Sound(string uri, int width, int height, int zindex)
            :this(uri, width, height, zindex, false)
        {
        }

		/// <summary>
		/// initialize a Sound renderer
		/// </summary>
		/// <param name="uri">URI of this Sound</param>
		/// <param name="width">Width in pixel</param>
		/// <param name="height">Height in pixel</param>
		/// <param name="zindex">Z-Index. The bigger the higher.</param>
        /// <param name="scale">Should the sound autoplay or not</param>
		public Sound(string uri, int width, int height, int zindex, bool autoplay)			
		{            
           
            mediaElement = new MediaElement();
			{
				mediaElement.Height = height;
				mediaElement.Width = width;
			}

            mediaElement.AutoPlay = autoplay;
            mediaElement.Source = new Uri(uri, UriKind.Relative);
            
		}
        /// <summary>
        /// Use this method to loop the sound
        /// </summary>
        /// <param name="isLoop"> Give the var to be true to loop it. To not loop the sound, just dont use this method</param>
        public void Loop(bool isLoop)
        {
            if (isLoop == true)
            {
                mediaElement.MediaEnded += new RoutedEventHandler(media_MediaEnded);
                //mediaElement.MediaOpened += new RoutedEventHandler(me_MediaOpened);
            }
        }
        /// <summary>
        /// Play sound 
        /// </summary>
        public void PlaySound()
        {
            mediaElement.Play();
        }
        /// <summary>
        /// Stop sound
        /// </summary>
        public void StopSound()
        {
            mediaElement.Stop();
        }
        /// <summary>
        /// Pause sound
        /// </summary>
        public void PauseSound()
        {
            mediaElement.Pause();
        }
        /// <summary>
        /// Set volume of the sound
        /// </summary>
        /// <param name="volume">Give floating point number for setting sound</param>
        public void setVolume(double volume)
        {
            mediaElement.Volume = volume;
           
        }

        public void media_MediaEnded(object sender, RoutedEventArgs e)
        {
            MediaElement ctr1 = (MediaElement)sender;
            // Setting the marker of first media element to near the beginning 
            ctr1.Position = TimeSpan.FromMilliseconds(0);
            ctr1.Play();
            
        }

        /// <summary>
        /// Rate at which volume goes to zero
        /// </summary>
        /// <param name="rate">floating point value of rate</param>
        public void CrossFade(double rate)
        {
            if (mediaElement.Volume >= 0)
            {
                mediaElement.Volume -= rate;
            }
        }

        public void me_MediaOpened(object sender, RoutedEventArgs e)
        {
            len = mediaElement.NaturalDuration;
            dp.Interval = len.TimeSpan;

            dp.Tick += new EventHandler(dp_Tick);
            dp.Start();
        }

        void dp_Tick(object sender, EventArgs e)
        {
            mediaElement.Position = TimeSpan.FromMilliseconds(0);
            mediaElement.Play();
        }
		
    }
}
